package com.sky.controller.user;

import com.sky.Entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "用户地址簿操作")
public class AddressBookController {
    @Autowired
    private AddressBookService service;

    @PostMapping
    @ApiOperation("新增地址")
    public Result<String> add(@RequestBody AddressBook addressBook){
        service.add(addressBook);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("查询用户的所有地址信息")
    public Result<List<AddressBook>> list(){
        List<AddressBook> list=service.list();
        return Result.success(list);
    }
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> defaultAddress(){
        AddressBook addressBook=service.defaultAddress();
        return Result.success(addressBook);
    }
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result<String> modifyAddress(@RequestBody AddressBook addressBook){
        service.modifyAddress(addressBook);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result<String> deleteAddress(Long id){
        service.deleteAddressById(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> queryAddressById(@PathVariable Long id){
        AddressBook addressBook=service.queryAddressById(id);
        return Result.success(addressBook);
    }
    @PutMapping("/default")
    @ApiOperation("根据id设置默认地址")
    public Result<String> setdefault(@RequestBody AddressBook addressBook){
        service.setdefault(addressBook);
        return Result.success();
    }

}
